<template>
    <view>
        <scroll-view :scroll-Y="true" :scroll-X="false">
            <view class="wuge" v-if="allGoods == '' || allGoods.length == 0">没有任何课程</view>
            <view class="list" v-for="(item, index) in allGoods" :key="index">
                <view class="item">
                    <view class="image" @tap="toDetail" :data-id="item.id">
                        <image :src="item.img" />
                    </view>
                    <view class="content">
                        <text class="h1">{{ item.name }}</text>
                        <text class="p myp">¥{{ item.price }}</text>
                        <text class="p" v-if="item.discount != null">折扣：{{ item.discount }}</text>
                    </view>
                    <view class="number">
                        <view class="plus" @tap="jian" :data-id="item.id">-</view>
                        <view class="sum">{{ item.buycount }}</view>
                        <view class="plus" @tap="jia" :data-id="item.id">+</view>
                    </view>
                </view>
            </view>
        </scroll-view>
        <view class="footer" v-if="sumPrice != 0">
            <view class="left">
                <text class="p">总价:</text>
                <text class="span">¥ {{ sumPrice }}</text>
            </view>
            <view class="right">
                <button @tap="settlement">去结算</button>
            </view>
        </view>
    </view>
</template>

<script>
var util = require('../../utils/util.js');
export default {
    data() {
        return {
            allGoods: {},
            sumPrice: 0
        };
    },
    onShow: function () {
        this.showAllGoods();
    },
    methods: {
        settlement: function () {
            uni.navigateTo({
                url: '/pages/settlement/index'
            });
        },

        jia: function (e) {
            this.jiaj(e, true);
        },

        jian: function (e) {
            this.jiaj(e, false);
        },

        showAllGoods: function () {
            var allGoods = uni.getStorageSync('shoppingcar');
            var sumPrice = 0;
            for (var i = 0; i < allGoods.length; i++) {                
                var price = allGoods[i].price;
                var count = allGoods[i].buycount;
                price = util.accMul(price, count);
                if (allGoods[i].discount && allGoods[i].discount > 0) {
                    var discount = allGoods[i].discount;
                    price = util.accMul(price, discount);
                }
                sumPrice = util.accAdd(sumPrice, price);
            }
            this.setData({
                allGoods: allGoods,
                sumPrice: sumPrice
            });
        },

        toDetail: function (e) {
            var id = e.currentTarget.dataset.id;
            uni.redirectTo({
                url: '../detail/index?id=' + id
            });
        },

        jiaj: function (e, boo) {
            var id = e.currentTarget.dataset.id;
            var s = 0;
            var allGoods = this.allGoods;
            for (var i = 0; i < allGoods.length; i++) {
                if (allGoods[i].id == id) {
                    if (boo) {
                        s = allGoods[i].buycount + 1;
                    } else {
                        s = allGoods[i].buycount - 1;
                    }
                    //最低值不得低于1
                    if (1 > s) {
                        allGoods.splice(i, 1);
                    } else {
                        allGoods[i].buycount = s;
                    }
                    break;
                }
            }
            uni.setStorageSync('shoppingcar', allGoods);
            this.setData({
                allGoods: allGoods
            });
            this.showAllGoods();
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
